﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Model;
using Resources;
using Service;

public partial class Software : System.Web.UI.Page
{


    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly StockService _StockService = new StockService();
    EquipmentSystem _system = new EquipmentSystem();
    private DirectService _DirectService = new DirectService();

    protected void Page_Load(object sender, EventArgs e)
    {

       // lblTotalSoftware.Text = "Total Software:" + _ReferenceDataService.GetSoftwares().Count;
       // lblTotalSoftware.Text = "Total software : " + new DirectService().GetSoftwareDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
       
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;
        grdSoftware.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;

        new Utility().SetRowToDisplay(grdSoftware, Session["rowsToSDisplay"]);
        if (!IsPostBack)
        {
            // to avoid loading for the first time

          
            if (Session["pageIndex"] != null)
            {
                grdSoftware.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }

            /* 
             * 1. number of equipments
             * 2. select the element 
             * */
           

        }
        else
        {
            txtHiddenPageIndex.Text = "0";
            //  SystemList = new DirectService().DataTableToIlistSystem(GetWhereClause(), GetOrderClause());
            lblTotalSoftware.Text = "Total software : " + new DirectService().GetSoftwareDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            int number = +new DirectService().GetSoftwareDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            if (number == 0)
            {
                grdSoftware1.Visible = true;
            }
            else
            {
                grdSoftware.Visible = true; 
                grdSoftware1.Visible = false;
            }
        }
        grdSoftware.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;
    }
    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;
        if (!string.IsNullOrEmpty(txtSoftwareId.Text))
        {
            if (int.TryParse(txtSoftwareId.Text, out i))
            {
                and = " and ";
                str = " where s.SoftwareId=" + Int32.Parse(txtSoftwareId.Text);
                if (txtSoftwareLabel.Text != string.Empty)
                {
                    str += " " + "and" + " " + "s.Software=" + "'" + txtSoftwareLabel.Text + "'";
                    and = " and ";
                }
            }else
            {
                grdSoftware.Visible = false;
                grdSoftware1.Visible = true;
                lblTotalSoftware.Text = "Total software : 0";
                return null;
            }
        }
        else
        {
            if (txtSoftwareLabel.Text != string.Empty)
            {
                str += " " + "where" + " " + "s.Software like" + "'%" + txtSoftwareLabel.Text + "%'";
                and = " and ";
            }
        }

        if (!String.IsNullOrEmpty(str))
            whereClause = str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by s.Software";
    }
   public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }
     
    protected void bntFilter_Click(object sender, EventArgs e)
    {
        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;
        ClearControlSearch();
    }
    private void ClearControlSearch()
    {
        txtSoftwareId.Text = "";
        txtSoftwareLabel.Text = ""; 
    }

    protected void bntValidate_Click(object sender, EventArgs e)
    {
        

    }

    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }

    protected void grdEquipment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        grdSoftware.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdSoftware.PageIndex.ToString();
        Session["pageIndex"] = grdSoftware.PageIndex;

    }
    protected void grdEquipment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdSoftware.EditIndex = -1;
    }
    protected void grdSystem_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("Delete"))
        {
            int softwareId = Convert.ToInt32(grdSoftware.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            //_StockService.DeleteSoftware(softwareId);
            _DirectService.DeleteSoftware(softwareId);
            Response.Redirect("Software.aspx");
        }
        new Utility().SetSelectedColorToGridView(e, grdSoftware);

    }
    protected void grdSystem_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        EquipmentSoftware  _software = new EquipmentSoftware();

        if (e.Cancel == false)
        {
            bool flag = true;
            if (flag == false)
            {
                GridViewRow row = grdSoftware.Rows[e.RowIndex];
                _software.SoftwareId = Int32.Parse(row.Cells[2].Text);
                _software.SoftwareLabel = row.Cells[4].Text;
            }
        }
    }
    protected void grdSystem_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void grdSystem_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }
    protected void grdSystem_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void grdEquipment_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grdSystem_Sorted(object sender, EventArgs e)
    {
        grdSoftware.DataBind();
    }
    protected void grdEquipment_RowDataBound1(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //block to add message
            
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                LinkButton deleteButton = (LinkButton)e.Row.Cells[1].Controls[0];
                deleteButton.Attributes.Add("onclick", "return confirm('" + Resource.ConfirmDeleteMessage + "?');");
            }
        }
    }
    protected void grdSystem1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void dsSoftware_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue != null) lblTotalSoftware.Text = "Total software:" + ((DataTable)e.ReturnValue).Rows.Count;
    }
}
